package ${packagename}.controller.api;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import ${packagename}.entity.${table.classname};
import ${packagename}.entity.conf.Conf${table.classname};
import ${packagename}.service.${table.classname}Service;
import ${packagename}.util.CheckUtil;
import ${packagename}.vo.BootstrapTableVoJA;
import ${packagename}.vo.R;

import tk.mybatis.mapper.util.StringUtil;

@Controller
@RequestMapping("/api/inter/${table.classname?uncap_first}")
public class ApiInter${table.classname}Controller {

	@Autowired
	private ${table.classname}Service ${table.classname?uncap_first}Service;
	
	@PostMapping("/add")
    @ResponseBody
	public R add(Model model, HttpServletRequest request, HttpServletResponse response,
		@ModelAttribute ${table.classname} entity){
		
		R retMsg;
		try {
			retMsg = ${table.classname?uncap_first}Service.add(entity);
		} catch (Exception e) {
			retMsg = R.error("服务器正在开小差，请联系客服");
		}
		
		return retMsg;
	}
	
	@PostMapping("/deleteFalse")
    @ResponseBody
	public R deleteFalse(Model model, HttpServletRequest request, HttpServletResponse response,Long ${table.pkjavaname}){
		
		if (!CheckUtil.notNull(${table.pkjavaname})) {
			return R.error("ID不能为空");
		}
		
		R retMsg;
		try {
			retMsg = ${table.classname?uncap_first}Service.deleteFalse(${table.pkjavaname});
		} catch (Exception e) {
			retMsg = R.error("服务器正在开小差，请联系客服");
		}
		
		return retMsg;
	}
	
	@PostMapping("/deleteTrue")
    @ResponseBody
	public R deleteTrue(Model model, HttpServletRequest request, HttpServletResponse response,
			Long ${table.pkjavaname}
			){
		
		if (!CheckUtil.notNull(${table.pkjavaname})) {
			return R.error("ID不能为空");
		}
		
		R retMsg = ${table.classname?uncap_first}Service.deleteTrue(${table.pkjavaname});
		
		return retMsg;
	}
	
	@PostMapping("/edit")
    @ResponseBody
	public R edit(Model model, HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute ${table.classname} entity){
		
		R retMsg;
		try {
			retMsg = ${table.classname?uncap_first}Service.edit(entity);
		} catch (Exception e) {
			retMsg = R.error("服务器正在开小差，请联系客服");
		}
		
		return retMsg;
	}
	
	@RequestMapping("/search")
    @ResponseBody
	public BootstrapTableVoJA search(Model model, HttpServletRequest request, HttpServletResponse response,
    		Integer size,
    		Integer page,
			String orderby,
			String sorttype,
			@ModelAttribute ${table.classname} e
			){
    	
		orderby = orderby == null ? orderby : StringUtil.camelhumpToUnderline(orderby).toLowerCase();
		sorttype = sorttype == null ? "desc" : sorttype;
		
		Conf${table.classname} con = new Conf${table.classname}();
		con.setLimit(size);
		con.setStart(page);
		con.setOrderby(orderby);
		con.setSorttype(sorttype);
		con.e = e;
		
		return ${table.classname?uncap_first}Service.search(con);
	}
}